var missionList=new Array();
var selectMissionList=new Array();
function getUserMissions(){
    //标记是否从搜索页面转来
    var bool=true;
    try {
        var search=getCookie("number");
    }catch (error) {
        search="";
        bool=false;
    }
    var data=search;
    $.ajax({
        type:"post",
        dataType:"text",
        url:"/fp_war_exploded/findmission",
        data:data,
        success:function (result) {
            result=result.replace(/[\r\n]/g,"");
            selectMissionList=result;
            document.cookie="selectResults="+result;
            if(bool){
                //window.location.href="missionPlatform.html";
                alert("getUserMissions:Line23"+result)
            }
        },
        error:function () {
            alert("getMission:17Line Search error");
        }
    });
}
function searchforMission() {
    //标记是否从搜索页面转来
    var bool=true;
    try {
        var search=document.getElementById("search").value;
    }catch (error) {
        search="";
        bool=false;
    }
    var data=search;
    $.ajax({
        type:"post",
        dataType:"text",
        url:"/fp_war_exploded/searchMission",
        data:data,
        success:function (result) {
            result=result.replace(/[\r\n]/g,"");

            selectMissionList=result;


            document.cookie="selectResults="+result;
            if(bool){
                window.location.href="missionPlatform.html";
            }
            //showSearchMission();
            //showMission();
            //alert("getMission.js success！"+result);
        },
        error:function (result) {
            alert("getMission:17Line Search error");
        }
    });
}
function searchGuangChangIndex() {
    //标记是否从搜索页面转来
    var bool=true;
    try {
        var search=document.getElementById("search").value;
    }catch (error) {
        search="";
        bool=false;
    }
    var data=search;
    $.ajax({
        type:"post",
        dataType:"text",
        url:"/fp_war_exploded/searchMission",
        data:data,
        success:function (result) {
            result=result.replace(/[\r\n]/g,"");
            selectMissionList=result;
            document.cookie="selectResults="+result;
            if(bool){
                //window.location.href="missionPlatform.html";
            }
            //showSearchMission();
            //showMission();
            //alert("getMission.js success！"+result);
        },
        error:function (result) {
            alert("87Line Search error");
        }
    });
}
function submitMission(){
        var userid=getCookie("Id");
        var title=document.getElementById("missionName").value;
        var author=getCookie("Name");
        var content=document.getElementById("Words").value;
        var place=document.getElementById("missionPlace").value;
        var quantity=document.getElementById("quantity").value;
        var reward=document.getElementById("energy").value;
    var data={
        userid:userid,
        title:title,
        author:author,
        content:content,
        place:place,
        quantity:quantity,
        reward:reward
    };
    $.ajax({
        type:"post",
        dataType:"text",
        url:"/fp_war_exploded/addMission",
        data:JSON.stringify(data),
        success:function (result) {
            alert("发布任务成功");
            window.location.href="index.html";
            //  alert("getMission.js success！")
        },
        error:function () {
            alert("error");
        }
    });
}
function getEnterKey(e){
    var keynum=e.keyCode;
    var key=e.which;
    if(keynum==13){
        searchforMission();
    }
}
function getCookie(cname) {
    var name = cname + "=";
    var decodedCookie = decodeURIComponent(document.cookie);
    var ca = decodedCookie.split(';');
    for(var i = 0; i <ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') {
            c = c.substring(1);
        }
        if (c.indexOf(name) == 0) {
            return c.substring(name.length, c.length);
        }
    }
    return "";
}
function createMissionIndex(personSend,missionTime,missionName,missionContent,energy,personAccept,personNumberA,personNumberS,check,price,place,missionId){
    var missions = document.getElementById("missions");
    var missionNew = document.createElement("div");
    var br = document.createElement('br');
    missionNew.className = "missionForm";
    missions.appendChild(missionNew);
    //开始添加form里边的东西
    myAppendChildClass(missionNew,"div","avatar","");
    myAppendChildClass(missionNew,"p","personNumberS",personNumberS);
    myAppendChildClass(missionNew,"p","personSend",personSend);
    myAppendChildClass(missionNew,"p","missionTime",missionTime);
    myAppendChildClass(missionNew,"p","missionMon","悬赏值:<span class='price'>"+energy+"</span>");
    myAppendChildClass(missionNew,"p","missionName",missionName);
    myAppendChildClass(missionNew,"p","missionContent",missionContent);
    myAppendChildClass(missionNew,"p","personNumberA",personNumberA);
    myAppendChildClass(missionNew,"p","personAccept",personAccept);
    myAppendChildClass(missionNew,"p","check",check);
    myAppendChildClass(missionNew,"p","place",place);
    var accept=myAppendChildClass(missionNew,"button","accept","接受");
    var think=myAppendChildClass(missionNew,"button","think","再想想/放弃");
    accept.setAttribute("id",missionId);
    var ac = document.getElementById(missionId);
    ac.onclick = function(event){
        acceptName(missionId);
        event.stopPropagation();
    }
    function acceptName(id){
        var sentence="accept";
        var recipient=getCookie("Number");
        var data={
            id:id,
            recipient:recipient,
            sentence:sentence
        };
        alert("this +"+id);
        $.ajax({
            type:"post",
            dataType:"text",
            url:"/fp_war_exploded/updateMission",
            data:JSON.stringify(data),
            success:function (result) {
                if (result.trim()=="success"){
                    alert("成功");
                }
                else{
                    alert("error");
                }
            },
            error:function (result) {
                alert("getMission error");
            }
        });

    }
}

function title(type,obj){
    var t = document.createElement(type);
    t.innerHTML = obj.innerHTML;
    return t;
}